/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  KarelCommand
 * 
 * Description of class:
 * 
 * Skeleton for commands that Karel the Robot can execute.
 * 
 */

package karel.gamelogic.commands;

import karel.gamelogic.world.KarelRobot;
import karel.graphics.Renderer;

public abstract class KarelCommand {

    protected KarelRobot robot;
    protected boolean commandDone;
    private Renderer renderer;
    
    private int maxCommandUpdatesPrSec = 240;
    
    protected KarelCommand(KarelRobot robot, Renderer renderer) {
        commandDone = false;
        this.robot = robot;
        this.renderer = renderer;        
    }
    
    final public boolean isCommandDone() {
        return commandDone;
    }
    
    final public void commandUpdate() {
        long commandStart = System.nanoTime();
        update();
        renderer.repaintScene();
        long commandTime = (System.nanoTime() - commandStart) / 1000000;
        try {
            Thread.sleep(Math.max(0, ((long) (1.0 / maxCommandUpdatesPrSec * 1000)) - commandTime));
        } catch (InterruptedException e) {
            Thread.interrupted();
        }
    }
    
    protected abstract void update();
    
    public abstract Object getCommandReturnValue();
    
}
